package com.jewel.service.impl;

import com.jewel.mapper.TabBlockMapper;
import com.jewel.mapper.TabItemMapper;
import com.jewel.model.Block;
import com.jewel.model.IndexItem;
import com.jewel.pojo.TabBlock;
import com.jewel.pojo.TabItem;
import com.jewel.pojo.TabItemExample;
import com.jewel.service.BlogService;
import com.jewel.tools.Tools;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 版区模块业务类
 * 
 * @author DELL
 *
 * @since 2017.04.27
 */
@Service
public class BlogServiceImpl implements BlogService{

    @Autowired
    private TabBlockMapper tabBlockMapper;

    @Autowired
    private TabItemMapper tabItemMapper;

    /**
	 * @Title: getBlogById
	 * @Description：获取指定id版区
	 * 
	 * @param blogId
	 * @return Block
	 * @throws：
	 * 
	 */
    @Override
    public Block getBlogById(int blogId) {
        Block block = null;

        TabBlock tabBlock = this.tabBlockMapper.selectByPrimaryKey(blogId);

        if (!Tools.objectIsNullOrNot(tabBlock)){
            block = new Block();

            block.setBlockId(tabBlock.getBlockId());
            block.setName(tabBlock.getName());
        }

        return block;
    }

    
    /**
	 * @Title: getAllItemByBlogId
	 * @Description：获取指定版区id所有产品
	 * 
	 * @param blogId
	 * @return List<IndexItem>
	 * @throws：
	 * 
	 */
    @Override
    public List<IndexItem> getAllItemByBlogId(int blogId) {
        List<IndexItem> list = null;

        TabItemExample example = new TabItemExample();
        TabItemExample.Criteria criteria = example.createCriteria();
        criteria.andBlockIdEqualTo(blogId);

        List<TabItem> result = this.tabItemMapper.selectByExample(example);

        if (!Tools.objectIsNullOrNot(result)){
            list = new ArrayList<IndexItem>();

            for (TabItem tabItem : result){
                IndexItem item = new IndexItem();

                item.setItemId(tabItem.getItemId());
                item.setName(tabItem.getTitle());
                item.setDescribes(tabItem.getDescribes());
                item.setImagePath(tabItem.getImagePath());
                item.setDateAdd(tabItem.getDateAdd());

                list.add(item);
            }
        }

        return list;
    }
}
